﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="RepresentativeMatter", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(RepresentativeMatter))]
    public class RepresentativeMatterSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IRepresentativeMatter clazz;
        
        public RepresentativeMatterSerializer(IRepresentativeMatter clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("OrderNumber");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "int");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdInt(((int)(clazz.OrderNumber))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("StateFeePetition");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "decimal");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDecimal(((decimal)(clazz.StateFeePetition))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("ObligorCase");
            if (clazz.ObligorCase!=null) {
                global::Xtee.Core.SerializationMetadata metaObligorCase = this.GetMetaData(clazz.ObligorCase);
                Writer.WriteTypeAttribute(metaObligorCase.TypeQualifiedName.Namespace, metaObligorCase.TypeQualifiedName.Name);
                metaObligorCase.NewSerializer(clazz.ObligorCase).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : ObligorCase");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("PetitionerConfirmation");
            if (clazz.PetitionerConfirmation!=null) {
                global::Xtee.Core.SerializationMetadata metaPetitionerConfirmation = this.GetMetaData(clazz.PetitionerConfirmation);
                Writer.WriteTypeAttribute(metaPetitionerConfirmation.TypeQualifiedName.Namespace, metaPetitionerConfirmation.TypeQualifiedName.Name);
                metaPetitionerConfirmation.NewSerializer(clazz.PetitionerConfirmation).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : PetitionerConfirmation");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("MutualResponsibility");
            if (clazz.MutualResponsibility!=null) {
                Writer.WriteTypeAttribute("http://producers.kis.xtee.riik.ee/producer/kis", "MutualResponsibility");
                Writer.WriteString(clazz.MutualResponsibility.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : MutualResponsibility");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("ConfirmDataCorrect");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "boolean");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdBoolean(((bool)(clazz.ConfirmDataCorrect))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("CollectibilityChoice");
            if (clazz.CollectibilityChoice!=null) {
                Writer.WriteTypeAttribute("http://producers.kis.xtee.riik.ee/producer/kis", "CollectibilityChoice");
                Writer.WriteString(clazz.CollectibilityChoice.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : CollectibilityChoice");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("StateFeePetitionTotal");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "decimal");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDecimal(((decimal)(clazz.StateFeePetitionTotal))).ToString());
            Writer.WriteEndElement();
            if (((clazz.DebtSettlement == null) 
                        || (clazz.DebtSettlement.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "DebtSettlement");
            }
            for (int x = 0; ((clazz.DebtSettlement != null) 
                        && (x < clazz.DebtSettlement.Count)); x = (x + 1)) {
                Writer.WriteStartElement("DebtSettlement");
                if (clazz.DebtSettlement[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaDebtSettlement = this.GetMetaData(clazz.DebtSettlement[x]);
                    Writer.WriteTypeAttribute(metaDebtSettlement.TypeQualifiedName.Namespace, metaDebtSettlement.TypeQualifiedName.Name);
                    metaDebtSettlement.NewSerializer(clazz.DebtSettlement[x]).WriteXml(Writer);
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : DebtSettlement");
                }
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="OrderNumber") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeMatter>OrderNumber");
                }
                clazz.OrderNumber = new global::Xtee.Core.Schema.XsdInt(Reader.ReadElementString("OrderNumber")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeMatter>OrderNumber", Reader.LocalName);
            }
            if (Reader.LocalName=="StateFeePetition") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeMatter>StateFeePetition");
                }
                clazz.StateFeePetition = new global::Xtee.Core.Schema.XsdDecimal(Reader.ReadElementString("StateFeePetition")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeMatter>StateFeePetition", Reader.LocalName);
            }
            if (Reader.LocalName=="ObligorCase") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeMatter>ObligorCase");
                }
                global::Xtee.Core.SerializationMetadata metaObligorCase = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorCase>(Reader, "ObligorCase", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.ObligorCase = metaObligorCase.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IObligorCase>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ObligorCase");
                    metaObligorCase.NewSerializer(clazz.ObligorCase).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeMatter>ObligorCase", Reader.LocalName);
            }
            if (Reader.LocalName=="PetitionerConfirmation") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeMatter>PetitionerConfirmation");
                }
                global::Xtee.Core.SerializationMetadata metaPetitionerConfirmation = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IPetitionerConfirmation>(Reader, "PetitionerConfirmation", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.PetitionerConfirmation = metaPetitionerConfirmation.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IPetitionerConfirmation>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("PetitionerConfirmation");
                    metaPetitionerConfirmation.NewSerializer(clazz.PetitionerConfirmation).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeMatter>PetitionerConfirmation", Reader.LocalName);
            }
            if (Reader.LocalName=="MutualResponsibility") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeMatter>MutualResponsibility");
                }
                clazz.MutualResponsibility = new global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.MutualResponsibility(Reader.ReadElementString("MutualResponsibility"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeMatter>MutualResponsibility", Reader.LocalName);
            }
            if (Reader.LocalName=="ConfirmDataCorrect") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeMatter>ConfirmDataCorrect");
                }
                clazz.ConfirmDataCorrect = new global::Xtee.Core.Schema.XsdBoolean(Reader.ReadElementString("ConfirmDataCorrect")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeMatter>ConfirmDataCorrect", Reader.LocalName);
            }
            if (Reader.LocalName=="CollectibilityChoice") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeMatter>CollectibilityChoice");
                }
                clazz.CollectibilityChoice = new global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.CollectibilityChoice(Reader.ReadElementString("CollectibilityChoice"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeMatter>CollectibilityChoice", Reader.LocalName);
            }
            if (Reader.LocalName=="StateFeePetitionTotal") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeMatter>StateFeePetitionTotal");
                }
                clazz.StateFeePetitionTotal = new global::Xtee.Core.Schema.XsdDecimal(Reader.ReadElementString("StateFeePetitionTotal")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeMatter>StateFeePetitionTotal", Reader.LocalName);
            }
            clazz.DebtSettlement = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDebtSettlement>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "DebtSettlement")); ) {
                if (Reader.LocalName=="DebtSettlement") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeMatter>DebtSettlement");
                    }
                    global::Xtee.Core.SerializationMetadata metaDebtSettlement = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDebtSettlement>(Reader, "DebtSettlement", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDebtSettlement item = metaDebtSettlement.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IDebtSettlement>();
                    clazz.DebtSettlement.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("DebtSettlement");
                        metaDebtSettlement.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeMatter>DebtSettlement", Reader.LocalName);
                }
            }
            if ((clazz.DebtSettlement.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "DebtSettlement");
            }
        }
    }
}
